//
//  QueryViewController.h
//  CommunityNannyO2O
//
//  Created by apple on 15/8/13.
//  Copyright (c) 2015年 Apple. All rights reserved.
//

#import "QueryViewController.h"
#import "APIAdress.h"
#import "MBProgressHUD+YC.h"
#import "MJExtension.h"
#import "AccountTool.h"
#import "AppConfig.h"
#import "QXNetWorkTool.h"
#import "CertificateQueryTableCell.h"
#import "CertificateQueryViewController.h"
#import "CertificateQueryModel.h"

@interface QueryViewController()<QXNetWorkToolDelegate,UISearchBarDelegate>

@property (weak, nonatomic) IBOutlet UISearchBar *bar;
@property (weak, nonatomic) IBOutlet UIButton *query;

@end

@implementation QueryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.bar.delegate = self;
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_query_search_bar.png"]];
    
    self.bar.backgroundImage = imageView.image;
    self.bar.layer.cornerRadius = 10;
    self.bar.layer.masksToBounds = YES;
    
    self.query.layer.cornerRadius = 5;
    self.query.layer.masksToBounds = YES;
    
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    
    [self.view addGestureRecognizer:tapGestureRecognizer];
}

-(void)configNavigationItem
{
    self.navigationItem.title = @"证书查询";
    [self.navigationController.navigationBar setTranslucent:NO];
    
    [self.navigationController.navigationBar
     setBackgroundImage:[UIImage imageNamed:@"bg_navigation_bar"]
     forBarMetrics:UIBarMetricsDefault];
    
    //status bar的字体为白色
    //导航栏的背景色是黑色
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0],UITextAttributeTextShadowColor,[UIFont fontWithName:@"Hiragino Sans GB" size:18.0],NSFontAttributeName,nil]];
    
    [self configNavigationBackAction:^{
        
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{

}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
    [self search];
}

-(void)tap
{
    [self.bar resignFirstResponder];
}

- (IBAction)query:(id)sender
{
    [self search];
}

- (void)search
{
    if ([self.bar.text isEqualToString:@""])
    {
        [MBProgressHUD showError:@"请输入证书编号"];
        return;
    }
    else
    {
        [MBProgressHUD showMessage:@"正在加载" toView:self.view];
        
        QXNetWorkTool *tool = [[QXNetWorkTool alloc]init];
        
        tool.QXNetWorkDelegate = self;
        
        [tool GetDataFromServerWithPostMethodandInterfaceName:CertificateQueryInterface andParameter:
                            @[@{@"certificate_num":self.bar.text}] tag:1];
    }
}

-(void)GetSuccessResponse:(id)Response andTag:(int)tag
{
    [MBProgressHUD hideHUDForView:self.view];

    switch (tag)
    {
        case 1:
        {
            for (NSDictionary *dic in Response)
            {
                CertificateQueryModel *model = [CertificateQueryModel CertificateQueryWithDict:dic];
    
                CertificateQueryViewController *obj =[CertificateQueryViewController create];
                
                obj.model = model;
                
                [self.navigationController pushViewController:obj animated:YES];
                break;
            }
        }
    }
}

- (void)GetFailResponse:(id)Response andTag:(int)tag
{
    [MBProgressHUD hideHUDForView:self.view];
    
    NSString *message = Response;
    
    [MBProgressHUD showError:message toView:self.view];
}

- (void)GetSuccessMessage:(id)message andTag:(int)tag
{
    [MBProgressHUD hideHUDForView:self.view];
    
    NSString *mes = message;
    
    switch (tag)
    {
        case 1:
            [MBProgressHUD showSuccess:mes toView:self.view];
            
            break;
    }
}


-(void)useMethodToFindBlackLineAndHind
{
    UIImageView* blackLineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    
    blackLineImageView.hidden = YES;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self configNavigationItem];
    
    [self useMethodToFindBlackLineAndHind];
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    

}



@end




















